package com.siecom.framework.module;

import android.util.Log;

import com.siecom.tools.ByteTool;

import java.util.zip.CRC32;

/**
 * Created by Administrator on 2015/12/15.
 */
public class SystemModule extends CommonModule{
    private static SystemModule module = null;
    private static final String TAG = "SystemModule";

    private SystemModule() {


    }

    public static SystemModule getInstance() {

        if (module == null) {
            module = new SystemModule();
        }
        return module;

    }
     public static int writeCodeName(String codeName){
         if(codeName.getBytes().length<=0||codeName.getBytes().length>16){
           return -6;
         }
         byte[] code = new byte[16];
         System.arraycopy(codeName.getBytes(),0,code,0,codeName.getBytes().length);
         return api.Sys_writeSN(code);
     }
    public static String readFingerVer(){
        byte[] code = new byte[128];
        byte[] type = new byte[1];
        int[]  len = new int[1];
        int ret = api.getFingerVersion(type,code,len);
        if(ret!=0){
            return "error";
        }
        String res = "";
        if(type[0]==(byte)0x00){
                res = "ZZ-"+ new String(code).trim();
        }
        else if(type[0]==(byte)0x01){
                res= "TC-"+ByteTool.bytearrayToHexString(code,len[0]);
        }
        else  if(type[0]==(byte)0x02){
             res= "WR-"+ByteTool.bytearrayToHexString(code,len[0]);
        }
        else  if(type[0]==(byte)0x03){ //中正SM80的
            res= "SM-"+ByteTool.bytearrayToHexString(code,len[0]);
        }else if (type[0] == (byte) 0x04){ //SM-2B_FM-205B-YNV1.0.8X-57600
            res = "ZZ-" + new String(code).trim();
        }else {
            res = "unknow type";
        }
        return res;
    }
    public static String readCodeName(){
        byte[] code = new byte[16];
        int ret = api.Sys_GetSN(code);
        if(ret!=0){
            return null;
        }
        return new String(code).trim();
    }

    public static int setDeviceNoTwo(String codeName){
        if(codeName.getBytes().length<=0||codeName.getBytes().length>16){
            return -6;
        }
        return api.Sys_writeSNTwo(codeName.getBytes());
    }

    public static String getDeviceNoTwo(){
        byte[] key = new byte[20];
        api.Sys_GetSNTwo(key);
        return new String(key).trim();
    }

    public static String readSerialNo(){
        byte[] SN = new byte[16];
        int ret = api.SysUniqId(SN);
        try {
            String rets = new String(SN, "ISO-8859-1");
            Log.e("GJ","序列号:"+ rets);
        } catch (Exception e) {
            // TODO: handle exception
        }
        CRC32 crc32 = new CRC32();
        crc32.reset();
        crc32.update(SN);
        StringBuffer buf = new StringBuffer(String.valueOf(crc32
                .getValue()));
        while (buf.length() < 16) {
            buf.append("0");
        }
        Log.e("readSerialNo:", "ret:"+buf);
        return "ZG"+buf;
    }
    public static String readSerialNoHN(){
        byte[] SN = new byte[16];
        int ret = api.SysUniqId(SN);
        try {
            String rets = new String(SN, "ISO-8859-1").trim();
            Log.e("GJ","序列号:"+ rets);
            return  rets;
        } catch (Exception e) {
            // TODO: handle exception
        }

//        Log.e("readSerialNo:", "ret:"+rets);
        return "ZG";
    }

    public static void beep(){
        new Thread(new Runnable() {
            @Override
            public void run() {
               api.Lib_Beep();
            }
        }).start();
    }
    public static String readFwVersion(){
        byte[] code = new byte[6];
        int ret = api.Sys_getFwVersion(code);
        if(ret!=0){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 3; i++) {
            sb.append(code[i]);
            if(i < 2){
                sb.append(".");
            }
        }
        return sb.toString();
    }

    public static String getRTC(){
        byte[] code = new byte[6];
        int ret = api.Sys_getRTC(code);
        if(ret!=0){
            return null;
        }
        return ByteTool.byte2hex(code);
    }
    public static int testPsamSlot(final byte slot){
        byte[] ATD = new byte[100];
        int ret = -1;
        try {
//            ret =api.Icc_Detect(slot);
            ret = api.Icc_Open(slot, (byte) 0x01, ATD);
            if (ret != 0) {
                //Log.e("Icc_Open", ret + "!!");
                return ret;
            }else{

                return 0;
            }
//            byte sendData[] = new byte[530];
//            byte cmd[] = new byte[4];
//            cmd[0] = 0x00; // 0-3 cmd
//            cmd[1] = (byte) 0xa4;
//            cmd[2] = 0x04;
//            cmd[3] = 0x00;
//            short Lc = 0x0e;
//            short Le = 256;
//            byte[] dataIn = new byte[512];
//            // 02 B7 07 00 14 00 00 A4 04 00 00 0E 31 50 41 59 2E 53 59 53 2E 44 44
//            // 46 30 B4 82
//            String sendmsg = "1PAY.SYS.DDF01";
//            sendData = sendmsg.getBytes();
//            System.arraycopy(sendData, 0, dataIn, 0, sendData.length);
//            APDU_SEND ApduSend = new APDU_SEND(cmd, Lc, dataIn, Le);
//            APDU_RESP ApduResp = new APDU_RESP();
//            ret = api.Icc_Command(slot, ApduSend, ApduResp);
//            Log.e("testPsamSlot[ run ]", "Command ret = " + ret);
        }finally {
            api.Icc_Close(slot);
        }
//        return ret;

    }

}
